//
//  CourseEvaluateUploadFileTool.m
//  优学荟管理系统
//
//  Created by 星道 on 17/7/14.
//  Copyright © 2017年 星道三好. All rights reserved.
//

#import "CourseEvaluateUploadFileTool.h"
#import "CourseEvaluateAudioView.h"
#import "EMCDDeviceManager.h"

@implementation CourseEvaluateUploadFileTool

// 0相册 1音频 2视频 3删除
- (void)ShowAlertWithSelect:(void(^)(NSInteger Item))Select
{
    UIAlertController *Alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"请选择操作" preferredStyle:(UIAlertControllerStyleActionSheet)];
    [Alert addAction:[UIAlertAction actionWithTitle:@"图片上传" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {

        Select (0);
    }]];
    [Alert addAction:[UIAlertAction actionWithTitle:@"音频上传" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {

        Select (1);
    }]];
    [Alert addAction:[UIAlertAction actionWithTitle:@"视频上传" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
        Select(3);
    }]];

    
    [Alert addAction:[UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil]];
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:Alert animated:YES completion:nil];
}

#pragma mark - 图片上传
// 上传图片
- (void)UploadImg:(UIImage *)Img Button:(UIButton *)Button Complete:(void(^)(NSString *ImgName))Complete
{
    [MBProgressHUD showHUD];
    [NetManager UpdataImgWithURL:LLBaseURLTeacher(@"upload.do") Img:Img parameter:@{@"action":@"uploadimage"} success:^(id obj) {

        NSString *Str = [[NSString alloc] initWithData:obj encoding:NSUTF8StringEncoding];

        dispatch_async(dispatch_get_main_queue(), ^{

            // 上传成功，设置图片
            Button.selected = YES;
            [Button setBackgroundImage:Img forState:(UIControlStateSelected)];
            Complete (Str);
        });

    } fail:^(NSError *error) {
        [MBProgressHUD showError:@"图片上传失败"];
    }];
}







#pragma mark - 音频上传
// 上传音频
- (void)UploadAVAudioWithButton:(UIButton *)Button Complete:(void(^)(NSString *ImgName))Complete
{
    [[UIApplication sharedApplication].keyWindow addSubview:[CourseEvaluateAudioView ConsumeSureWithClickCommit:^(BOOL IsSelect) {

        // 录音切换
        IsSelect ? [self MgrBeginAudio]:[self MgrCloseAudio:Button Complete:Complete];
    }]];
}

/**
 开始录音
 */
- (void)MgrBeginAudio
{
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"download/aaa"];

    [[EMCDDeviceManager sharedInstance] asyncStartRecordingWithFileName:path completion:^(NSError *error)
     {
     }];

}

/**
 停止录间并上传
 */
- (void)MgrCloseAudio:(UIButton *)Button Complete:(void(^)(NSString *ImgName))Complete
{
    [[EMCDDeviceManager sharedInstance] asyncStopRecordingWithCompletion:^(NSString *recordPath, NSInteger aDuration, NSError *error) {

        if (recordPath.length != 0) {
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [MBProgressHUD showHUD];
                [NetManager UpdataAudioWithURL:LLBaseURLTeacher(@"upload.do") FilePath:recordPath parameter:@{@"action":@"uploadimage"} success:^(id obj) {
                    
                    NSString *Str = [[NSString alloc] initWithData:obj encoding:NSUTF8StringEncoding];
                    dispatch_async(dispatch_get_main_queue(), ^{
                        
                        // 上传成功，设置图片
                        [Button setBackgroundImage:[UIImage imageNamed:@"btn_mp3"] forState:(UIControlStateNormal)];
                        Complete (Str);
                    });
                    
                } fail:^(NSError *error) {
                    [MBProgressHUD showError:@"音频上传失败"];
                }];
            });
        }
        
        // 主线程更新图片
        

    }];
}


// 判断是否为空
- (BOOL)JudgeWithaction:(NSString *)action
              imgFileId:(NSString *)imgFileId
               lessonId:(NSString *)lessonId
              subjectId:(NSString *)subjectId
              childName:(NSString *)childName
           commonFileId:(NSString *)commonFileId
          enrollInfoIds:(NSString *)enrollInfoIds
          commonProblem:(NSString *)commonProblem
         personalFileId:(NSString *)personalFileId
        personalProblem:(NSString *)personalProblem
     ThreeGoodSelectArr:(NSMutableArray *)ThreeGoodSelectArr
   ThreeGoodSelectIndex:(NSInteger)ThreeGoodSelectIndex
{
//    if (imgFileId == nil || imgFileId.length <= 0)
//    {
//        [MBProgressHUD showError:@"请至少选择一张作业图表的图片"];
//        return NO;
//    }

    if (childName == nil || childName.length <= 0)
    {
        [HUD showError:@"请选择学生账号"];
        return NO;
    }

//    if (commonProblem == nil || commonProblem.length <= 0)
//    {
//        [MBProgressHUD showError:@"请输入共性问题"];
//        return NO;
//    }
//
//    if (personalProblem == nil || personalProblem.length <= 0)
//    {
//        [MBProgressHUD showError:@"请输入个性问题"];
//        return NO;
//    }

    if (ThreeGoodSelectArr == nil || ThreeGoodSelectArr.count != ThreeGoodSelectIndex)
    {
        [HUD showError:@"请完善三好选项"];
        return NO;
    }
    return YES;
}
@end
